﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityFramework.Extensions;

namespace DAO
{
    public class ChatRoomDAO
    {
        WebChatDBEntities db = new WebChatDBEntities();

        public void AddChatRoom(ChatRoom room)
        {
            db.ChatRooms.Add(room);
            db.SaveChanges();
        }

        public bool DeleteChatRoom(int id)
        {
            ChatRoom room = db.ChatRooms.Find(id);
            if(room == null)
                return false;

            RoomRequestDAO roomRequestDAO = new RoomRequestDAO();
            RoomMemberDAO roomMemberDAO = new RoomMemberDAO();
            MessageDAO messageDAO = new MessageDAO();

            roomRequestDAO.DeleteAllRequestsOfOneRoom(id);
            roomMemberDAO.DeleteAllMembersOfOneRoom(id);
            messageDAO.DeleteAllMessagesOfOneRoom(id);

            db.ChatRooms.Remove(room);
            db.SaveChanges();

            return true;
        }

        public void DeleteChatRoomByUserId(int userId)
        {
            db.ChatRooms.Delete(r => r.HostUserId == userId);

            db.SaveChanges();
        }
    }
}
